/**
  ******************************************************************************
  * @file    py32f0xx_it.c
  * @author  MCU Application Team
  * @Version V1.0.0
  * @Date    2020-10-19
  * @brief   Interrupt Service Routines.
  ******************************************************************************

  */

/* Includes ------------------------------------------------------------------*/

#include "py32f0xx_it.h"
#include "main.h"
#include "mcu_peripheral.h"
#include "six_step_cmp.h"
#include "six_step_fsm.h"

/* Private includes ----------------------------------------------------------*/
extern ADC_HandleTypeDef    AdcHandle;
/* Private typedef -----------------------------------------------------------*/

 void ClearFlag(TIM_TypeDef*TIM,uint32_t value)
{
  CLEAR_BIT(TIM->SR, value);
}
/* Private define ------------------------------------------------------------*/


/* Private macro -------------------------------------------------------------*/


/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/


/* Private user code ---------------------------------------------------------*/


/* External variables --------------------------------------------------------*/


/******************************************************************************/
/*           Cortex-M0+ Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
    MC_SixStep_Parameters.MotorErrCode = MOTOR_HARDFAULT_ERROR;
    MC_6StepCmp_ErrHandle();
}


/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
}


/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{	
	MC_6StepCmp_1msTimerUpdateIsr();
	HAL_IncTick();
}

/******************************************************************************/
/* PY32F0xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_py32f003xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles WWDG Interrupt .
  */
void WWDG_IRQHandler(void)
{
}
void DMA1_Channel1_IRQHandler(void)
{
	MC_SixStep_Parameters.IDC_AdcResult	= AdcData[ADC_CH_IDC]; //0
	MC_SixStep_Parameters.NTC_AdcResult = AdcData[ADC_CH_NTC]; //1
	MC_SixStep_Parameters.BAT_AdcResult = AdcData[ADC_CH_UDC]; //6

	if(DMA1->ISR & DMA_FLAG_TC1)DMA1->IFCR |= DMA_FLAG_TC1;
}

void ADC_COMP_IRQHandler(void)
{
	if(READ_BIT(EXTI->PR, EXTI_PR_PR17))
	{	

		#ifdef UART_DEBUG
		printf("MOTOR_HOC_ERROR\r\n");
		#endif
		MC_SixStep_Parameters.MotorErrCode = MOTOR_HOC_ERROR;
		MC_6StepCmp_ErrHandle();

		SET_BIT(EXTI->PR, EXTI_PR_PR17);

	}
//	if(READ_BIT(EXTI->PR, EXTI_PR_PR18)) 
//	{
//		SET_BIT(EXTI->PR, EXTI_PR_PR18);
//	}
}

void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
  	__disable_irq();
	
//	LedCtrl_SET_TOGGLE;
	
	MC_6StepCmp_TimerUpdateIsr();
	
	ClearFlag(TIM1,TIM_SR_UIF);

	__enable_irq();

}

void TIM1_CC_IRQHandler(void)
{
	LedCtrl_SET_TOGGLE;
	ClearFlag(TIM1,TIM_SR_CC4IF);	
}


void TIM17_IRQHandler(void)
{
//	LedCtrl_SET_TOGGLE;

	TIM17->CNT = 0;
	TIM17->DIER &= ~TIM_IT_UPDATE;
	MC_6StepCmp_CommunicatingPhase();
	MC_SixStep_Parameters.BLDC_State=BLDC_STAGE_FREE_WHEL;

	ClearFlag(TIM17,TIM_SR_UIF);
	
//	LedCtrl_SET_TOGGLE;
}







